#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include "dbTree.h"
#include "dbBase.h"
#include "dbTag.h"
#include "extMD5.h"
using namespace std;
using namespace db;

Base tBase;

enum Mode
{
  LIST,
  NEWPOST
};

int mode = LIST;
char cmd;

int main (int argc, char const* argv[])
{
while (true)
{
  string command, file, pass;
  cin >> command;
  
  
  if (command == "save")
  {
    cin >> file >> pass;
	tBase.setPass(pass);
	if (-1 == tBase.Save((file+".lgt").c_str()))
		cout << "blad przy otwieraniu pliku" << endl;
	else
		cout << "zapis zakonczony powodzeniem" << endl;
	continue;
  }
  else if (command == "load")
  {
	cin >> file >> pass;
	tBase.setPass(pass);
	if (-1 == tBase.Load((file+".lgt").c_str()))
		cout << "bledne haslo" << endl;
	else
		cout << "ladowanie zakonczone powodzeniem" << endl;
	continue;
  }
  else if (command == "quit")
	  return 0;
  else
	  cout << "nieznane polecenie" << endl;
}

  while (false)
  {
    //system("clear");
    cout << "devLOGTREE 0.1.1.4" << endl << endl;
    
      if (mode == LIST)
      {
	if (!tBase.atRoot())
	{
	  cout << "Title:\t" << tBase.getCurrent()->getTitle() << endl;
	  cout << "Date:\t" << tBase.getCurrent()->getDate() << endl;
	  cout << tBase.getCurrent()->getText() << endl << endl;
	}

      for (int i = 0; i < tBase.getCurrent()->getBranchCount(); i++)
      {
	if (!(tBase.atLeaf() && i == 0))
	{
	  if (i == 0)
	    cout << "Next: (" << tBase.moveSide(i)->getID() << ") ";
	  else 
	    cout << i << ": (" << tBase.moveSide(i)->getID() << ") ";			
	  cout << tBase.moveSide(i)->getTitle() << endl;
	}
      }

      for (int i = 0; i < tBase.getCurrent()->getTagsCount(); i++)  
	cout << tBase.getCurrent()->getTag(i) << " ";      
      cout << endl;
      
      cout << "j: next\tk: previous\t";
      cout << "l ID: go to child\ta: add post\tt: add tag\tr: rem tag" << endl;
      cout << "T: show tags" << endl;
      cout << "d WITH_CHILDREN WITH_SUCCESSOR: delete post" << endl;
      cout << "q: quit" << endl;

      while (true)
      {
	cin >> cmd;
	if (cmd == 'j')
	{
	  tBase.goTo(tBase.moveDown());
	  break;
	}
	else if (cmd == 'k')
	{
	  tBase.goTo(tBase.moveUp());
	  break;
	}
	else if (cmd == 'l')
	{
	  int r; 
	  cin >> r;			
	  tBase.goTo(tBase.moveSide(r));
	  break;
	}
	else if (cmd == 'a')
	{
	  mode = NEWPOST;
	  break;
	}
	else if (cmd == 'd')
	{
	  bool q, r;
	  cin >> q >> r;
	  tBase.delCurrentEntry(q, r);
	  break;
	}
	else if (cmd == 't')
	{
	  string tag;
	  cin >> tag;
	  tBase.getCurrent()->addTag(tag);
	  break;
	}
	else if (cmd == 'r')
	{
	  string tag;
	  cin >> tag;
	  tBase.getCurrent()->remTag(tag);
	  break;
	}
	else if (cmd == 'T')
	{
	  for (int i = 0; i < tBase.getTagsCount(); i++)
	  {
	    cout << tBase.getTag(i)->getName() << endl;
	    for (int j = 0; j < tBase.getTag(i)->getAppearsCount(); j++)
	      cout << tBase.getTag(i)->getAppear(j)->getTitle() << endl;
	    cout << endl;
	  }
	  break;
	}
	else if (cmd == 'q')
	{
	  return 0;
	}
	else
	{
	  continue;
	}
      }
    }
    else if (mode == NEWPOST)
    {
      cout << "e: add post at the end" << endl;
      cout << "a: add post after current" << endl;
      cout << "b: add a branch" << endl;
      cout << "c: cancel" << endl;
      while (true)
      {
	cin >> cmd;
	cin.get();
	if (cmd == 'e' || cmd == 'a' || cmd == 'b')
	{
	  char title[256], text[1024];
	  if (cmd == 'e')
	    tBase.goTo(tBase.addEndEntry());
	  else if (cmd == 'a')
	    tBase.goTo(tBase.addEntry());
	  else	
	    tBase.goTo(tBase.addBranch());
	  
	  cout << "TITLE: ";
	  cin.getline(title, 256);
	  tBase.getCurrent()->setTitle(title);

	  cout << " TEXT: ";
	  cin.getline(text, 1024);
	  tBase.getCurrent()->setText(text);

	  mode = LIST;
	  break;
	}
	else if (cmd == 'c')
	{
	  mode = LIST;
	  break;
	}
	else
	{
	  continue;
	}
      }
    }
  }

  return 0;
}
